import {mysqlTable, datetime, int, varchar, tinyint, text} from 'drizzle-orm/mysql-core'
import {sql} from 'drizzle-orm'

// ip表
export const sysIp = mysqlTable('sys_ip', {
  createTime: datetime('create_time', {mode: 'string'}).default(sql`CURRENT_TIMESTAMP`),
  updateTime: datetime('update_time', {mode: 'string'}).$onUpdate(() => sql`CURRENT_TIMESTAMP`),
  id: int('id').primaryKey().autoincrement().notNull(),
  ip: varchar('ip', {length: 255}).notNull().unique(),
  country: varchar('country', {length: 255}),
  prov: varchar('prov', {length: 255}),
  city: varchar('city', {length: 255}),
  district: varchar('district', {length: 255}),
  street: varchar('street', {length: 255}),
  lng: varchar('lng', {length: 255}),
  lat: varchar('lat', {length: 255}),
  isp: varchar('isp', {length: 255}).notNull(),
  desc: varchar('desc', {length: 255}).notNull(),
  zipcode: varchar('zipcode', {length: 255}),
})

// 登录日志表
export const sysLoginLog = mysqlTable('sys_login_log', {
  createTime: datetime('create_time', {mode: 'string'}).default(sql`CURRENT_TIMESTAMP`),
  id: int('id').primaryKey().autoincrement().notNull(),
  userId: int('user_id').references(() => sysUser.id, {onDelete: 'restrict', onUpdate: 'restrict'}),
  ip: varchar('ip', {length: 255}),
  address: varchar('address', {length: 255}),
  browser: varchar('browser', {length: 255}),
  os: varchar('os', {length: 255}),
  status: tinyint('status').notNull(),
  reason: varchar('reason', {length: 255}),
  online: tinyint('online').notNull(),
})

// 请求日志表
export const sysReqLog = mysqlTable('sys_req_log', {
  createTime: datetime('create_time', {mode: 'string'}).default(sql`CURRENT_TIMESTAMP`),
  id: int('id').primaryKey().autoincrement().notNull(),
  ip: varchar('ip', {length: 20}),
  address: varchar('address', {length: 20}).notNull(),
  userId: int('user_id').references(() => sysUser.id, {onDelete: 'restrict', onUpdate: 'restrict'}),
  params: text('params'),
  paramsType: varchar('params_type', {length: 10}),
  url: varchar('url', {length: 100}),
  method: varchar('method', {length: 15}),
  status: int('status'),
  consumeTime: int('consume_time').default(0),
})

// 菜单表
export const sysMenu = mysqlTable('sys_menu', {
  id: int('id').primaryKey().autoincrement().notNull(),
  createTime: datetime('create_time', {mode: 'string'}).default(sql`CURRENT_TIMESTAMP`),
  updateTime: datetime('update_time', {mode: 'string'}).$onUpdate(() => sql`CURRENT_TIMESTAMP`),
  pid: int('parent_id'),
  name: varchar('name', {length: 255}).notNull(),
  perms: varchar('perms', {length: 255}),
  type: tinyint('type').default(0).notNull(),
  icon: varchar('icon', {length: 255}),
  order: int('order_num').default(0),
  isShow: tinyint('is_show').default(1),
})

// 角色表
export const sysRole = mysqlTable('sys_role', {
  createTime: datetime('create_time', {mode: 'string'}).default(sql`CURRENT_TIMESTAMP`),
  updateTime: datetime('update_time', {mode: 'string'}).$onUpdate(() => sql`CURRENT_TIMESTAMP`),
  id: int('id').primaryKey().autoincrement().notNull(),
  name: varchar('name', {length: 255}).notNull(),
  label: varchar('label', {length: 50}).notNull(),
  remark: varchar('remark', {length: 255}),
})

// 角色菜单关系表
export const sysRoleMenu = mysqlTable('sys_role_menu', {
  createTime: datetime('create_time', {mode: 'string'}).default(sql`CURRENT_TIMESTAMP`),
  updateTime: datetime('update_time', {mode: 'string'}).$onUpdate(() => sql`CURRENT_TIMESTAMP`),
  id: int('id').primaryKey().autoincrement().notNull(),
  roleId: int('role_id')
    .notNull()
    .references(() => sysRole.id, {onDelete: 'restrict', onUpdate: 'restrict'}),
  menuId: int('menu_id')
    .notNull()
    .references(() => sysMenu.id, {onDelete: 'restrict', onUpdate: 'restrict'}),
})

// 用户表
export const sysUser = mysqlTable('sys_user', {
  createTime: datetime('create_time', {mode: 'string'}).default(sql`CURRENT_TIMESTAMP`),
  updateTime: datetime('update_time', {mode: 'string'}).$onUpdate(() => sql`CURRENT_TIMESTAMP`),
  id: int('id').primaryKey().autoincrement().notNull(),
  name: varchar('name', {length: 255}).notNull(),
  username: varchar('username', {length: 255}).notNull(),
  password: varchar('password', {length: 64}).notNull(),
  avatar: varchar('avatar', {length: 255}),
  email: varchar('email', {length: 255}),
  phone: varchar('phone', {length: 255}),
  remark: varchar('remark', {length: 255}),
  pSalt: varchar('p_salt', {length: 32}).notNull(),
  iSalt: varchar('i_salt', {length: 32}).notNull(),
  nickName: varchar('nick_name', {length: 255}),
})

// 用户角色关系表
export const sysUserRole = mysqlTable('sys_user_role', {
  createTime: datetime('create_time', {mode: 'string'}).default(sql`CURRENT_TIMESTAMP`),
  updateTime: datetime('update_time', {mode: 'string'}).$onUpdate(() => sql`CURRENT_TIMESTAMP`),
  id: int('id').primaryKey().autoincrement().notNull(),
  userId: int('user_id')
    .notNull()
    .references(() => sysUser.id, {onDelete: 'restrict', onUpdate: 'restrict'}),
  roleId: int('role_id')
    .notNull()
    .references(() => sysRole.id, {onDelete: 'restrict', onUpdate: 'restrict'}),
})
